<script setup>
import { getGoodsDetailAPI, getGoodsRelevantAPI } from '@/api'
import { useRoute } from 'vue-router'
import { provide, ref } from 'vue'
import goodsBread from './components/goodsBread.vue'
import goodsImage from './components/goodsImage.vue'
import goodsSale from './components/goodsSale.vue'
import goodsName from './components/goodsName.vue'
import goodsSku from './components/goodsSku.vue'
import goodsRelevant from './components/goodsRelevant.vue'
import goodsTabs from './components/goodsTabs.vue'
import goodsHot from './components/goodsHot.vue'
import goodsWarn from './components/goodsWarn.vue'

const route = useRoute()
const goodsId = ref('')
goodsId.value = route.params.id
provide('goodsId', goodsId)
const goodsList = ref({})

const getGoodsDetailFn = async() => {
  const res = await getGoodsDetailAPI(route.params.id)
  console.log(res)
  goodsList.value = res.result

  // window.goodsList = res.result
}
getGoodsDetailFn()

// const skuId = ref('')
// skuId.value = goodsList.value.skus.filter(item => item.id)

const num = ref(1)

const RelevantList = ref([])
const list = ref([])
const getGoodsRelevantFn = async() => {
  const res = await getGoodsRelevantAPI(route.params.id)
  console.log(res)
  RelevantList.value = res.result
  // window.goodsList = res.result

  const size = 4
  const n = Math.floor(RelevantList.value.length / size)
  console.log(n)
  for (let i = 0; i < n; i++) {
    const t = RelevantList.value.slice(i * size, i * size + size)
    list.value.push(t)
  }
}
getGoodsRelevantFn()

</script>
<template>
  <div class='xtx-goods-page'>
    <div class="container">
      <goodsBread :goodsList="goodsList"></goodsBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <!--左边-->
        <div class="media">
          <!-- 区域1 商品图片组件 -->
        <goodsImage :image="goodsList.mainPictures"></goodsImage>
          <!-- 区域2 商品销售信息组件 -->
          <goodsSale></goodsSale>
        </div>
        <!--右边-->
        <div class="spec">
          <!-- 区域3 商品介绍组件 -->
          <goodsName :goods="goodsList"></goodsName>
          <!-- 区域4 商品规格组件  -->
          <goodsSku :goods="goodsList" ></goodsSku>
          <!-- 区域5 数量选择组件 -->
          <numbox v-model="num" :min="1" :label="'数量'" ></numbox>
          <!-- 加入购物车 按钮 -->
          <xtxbutton type="primary" style="margin-top: 20px">加入购物车</xtxbutton>
        </div>
      </div>
      <!-- 商品推荐 -->
  <div class='goods-relevant'>

  <goodsRelevant :RelevantList="RelevantList" :list="list"></goodsRelevant>
  </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <goodsTabs :goodsList="goodsList"></goodsTabs>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goodsWarn></goodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goodsHot :type="1" :goodsId="goodsId"></goodsHot>
          <goodsHot :type="2" :goodsId="goodsId"></goodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

.number-box {
  display: flex;
  align-items: center;
  .label {
    width: 60px;
    color: #999;
    padding-left: 10px;
  }
}
.g-name {
  font-size: 22px;
}
.g-desc {
  color: #999;
  margin-top: 10px;
}
.g-price {
  margin-top: 10px;
  span {
    &::before {
      content: "¥";
      font-size: 14px;
    }
    &:first-child {
      color: @priceColor;
      margin-right: 10px;
      font-size: 22px;
    }
    &:last-child {
      color: #999;
      text-decoration: line-through;
      font-size: 16px;
    }
  }
}
.g-service {
  background: #f5f5f5;
  width: 500px;
  padding: 20px 10px 0 10px;
  margin-top: 10px;
  dl {
    padding-bottom: 20px;
    display: flex;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      color: #666;
      &:last-child {
        span {
          margin-right: 10px;
          &::before {
            content: "•";
            color: @xtxColor;
            margin-right: 2px;
          }
        }
        a {
          color: @xtxColor;
        }
      }
    }
  }
}
.goods-sales {
  display: flex;
  width: 400px;
  align-items: center;
  text-align: center;
  height: 140px;
  li {
    flex: 1;
    position: relative;
    ~ li::after {
      position: absolute;
      top: 10px;
      left: 0;
      height: 60px;
      border-left: 1px solid #e4e4e4;
      content: "";
    }
    p {
      &:first-child {
        color: #999;
      }
      &:nth-child(2) {
        color: @priceColor;
        margin-top: 10px;
      }
      &:last-child {
        color: #666;
        margin-top: 10px;
        i {
          color: @xtxColor;
          font-size: 14px;
          margin-right: 2px;
        }
        &:hover {
          color: @xtxColor;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
